package com.lajiaomian.springbootruijiwaimai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lajiaomian.springbootruijiwaimai.common.R;
import com.lajiaomian.springbootruijiwaimai.dto.SetmealDto;
import com.lajiaomian.springbootruijiwaimai.pojo.Category;
import com.lajiaomian.springbootruijiwaimai.pojo.Dish;
import com.lajiaomian.springbootruijiwaimai.pojo.Setmeal;
import com.lajiaomian.springbootruijiwaimai.pojo.SetmealDish;
import com.lajiaomian.springbootruijiwaimai.service.impl.CategoryService;
import com.lajiaomian.springbootruijiwaimai.service.impl.SetmealDishService;
import com.lajiaomian.springbootruijiwaimai.service.impl.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lajiaomian
 * @create 2022-08-14-21:42
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 获取分页的setmeal信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> getPage(Integer page,Integer pageSize,String name){
        //注意：这里要分页的还是SetmealDto这种联合类
        Page pageInfo = new Page(page,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotEmpty(name),"name",name);
        setmealService.page(pageInfo, wrapper);
        //新建一个SetmalDtoPage
        Page SetmealDtoPage = new Page();
        //将SetmealDto的值赋给SetmealPage除了records
        BeanUtils.copyProperties(pageInfo,SetmealDtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = new ArrayList<>();
        for(Setmeal item:records){
            //新建一个SetmealDto
            SetmealDto setmealDto = new SetmealDto();
            //将item的值赋给SetmealDto
            BeanUtils.copyProperties(item,setmealDto);
            //获取每个item的categoryId
            Long categoryId = item.getCategoryId();
            //根据categoryId查询category对象
            Category category = categoryService.getById(categoryId);
            //获取category 的name值
            String categoryName = category.getName();
            //给setmalDto设置上categoryName
            setmealDto.setCategoryName(categoryName);
            list.add(setmealDto);
        }
        SetmealDtoPage.setRecords(list);
        return R.success(SetmealDtoPage);
    }

    /**
     * 保存setmeal
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("保存套餐，{}",setmealDto.toString());
        setmealService.addSetmealDto(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 根据id查询setmeal信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getBySetmealId(@PathVariable("id")Long id){
        log.info("查询setmeal，id：{}",id);
        SetmealDto setmealDto = setmealService.getSetmealDtoByid(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐信息
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("更新成功");
    }

    /**
     * 禁用套餐
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> updateStatus0(Long[] ids){
        log.info("状态修改，ids：{}",ids);
        for(Long id : ids){
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(0);
            setmealService.updateById(setmeal);
        }
        return R.success("状态修改成功");
    }

    /**
     * 启用套餐
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> updateStatus1(Long[] ids){
        log.info("状态修改，ids：{}",ids);
        for(Long id : ids){
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(1);
            setmealService.updateById(setmeal);
        }
        return R.success("状态修改成功");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long[] ids){
        for (Long id:
             ids) {
            setmealService.removeById(id);
        }
        return R.success("删除成功");
    }

    /**
     * 展示套餐属性
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R<List<SetmealDto>> list(Long categoryId,Integer status){
        //先获取setmeal的属性
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId).orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置categoryName属性和setmealDishesList属性
        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            Long setmealId = setmeal.getId();
            lambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealId);
            List<SetmealDish> setmealDishList = setmealDishService.list(lambdaQueryWrapper);
            lambdaQueryWrapper.clear();
            setmealDto.setSetmealDishes(setmealDishList);
            return setmealDto;
        }).collect(Collectors.toList());
        return R.success(setmealDtoList);
    }
}
